﻿/*
 * Author: Mattan360
 * Contributors:
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Irrlicht.Net
{
    public class ReferenceCounted : INative, IDisposable
    {
        #region PInvoke
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern bool ReferenceCounted_Drop(IntPtr rc);
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ReferenceCounted_Grab(IntPtr rc);
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern int ReferenceCounted_GetReferenceCount(IntPtr rc);
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern IntPtr ReferenceCounted_GetDebugName(IntPtr rc);
        #endregion

        public ReferenceCounted()
        { }
        public ReferenceCounted(IntPtr pointer)
        {
            this.pointer = pointer;
        }

        /// <summary>
        /// Drops the object. Decrements the reference counter by one.
        /// </summary>
        public void Dispose()
        {
            ReferenceCounted_Drop(pointer);
        }

        /// <summary>
        /// Grabs the object. Increments the reference counter by one.
        /// </summary>
        public void Grab()
        {
            ReferenceCounted_Grab(pointer);
        }

        /// <summary>
        /// Get the reference count.
        /// </summary>
        public int ReferenceCount
        {
            get
            {
                return ReferenceCounted_GetReferenceCount(pointer);
            }
        }

        /// <summary>
        /// Gets and sets the debug name of the object.
        /// </summary>
        public string DebugName
        {
            get
            {
                return Marshal.PtrToStringAnsi(ReferenceCounted_GetDebugName(pointer));
            }
        }
    }
}
